package com.heima.behavior.listener;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.dto.FollowBehaviorDto;
import com.heima.behavior.service.IApFollowBehaviorService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/4/17 15:18
 * @Version 1.0
 */


@Service
public class FollowListener {

    @Autowired
    private IApFollowBehaviorService followBehaviorService;

    @KafkaListener(topics = "${topic.followBehaviorTopic}")
    public void handleMessage(ConsumerRecord<String, String> record) {
        System.out.println("接收到消息:  value = " + record.value());
        // 将JSON消息转换成dto
        FollowBehaviorDto dto = JSON.parseObject(record.value(), FollowBehaviorDto.class);
        followBehaviorService.saveFollow(dto);
    }
}
